package orz.itrip.auth.controller;

import io.swagger.annotations.*;
import org.springframework.mobile.device.Device;
import org.springframework.web.bind.annotation.*;
import orz.itrip.auth.service.UserService;
import orz.itrip.dto.Dto;
import orz.itrip.pojo.User;
import orz.itrip.vo.ItripToken;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api")
@Api(tags = "手机/邮箱注册",description = "文档说明",hidden = true)
public class UserController {

    @Resource
    private UserService userService;
    //判断邮箱是否已经存在
    @GetMapping("/ckusr")
    @ApiOperation(value="验证账号是否存在",notes="用于验证注册账号是否已经注册")
    public Dto isExists(String name){
        return userService.isExists(name);
    }
    //邮箱注册功能
    @PostMapping("/doregister")
    @ApiOperation(value="邮箱注册")
    public Dto regeditByEmail(@RequestBody User user) throws MessagingException {
        return userService.regedit(user);
    }

    //邮箱/手机激活
    @PutMapping("/active")
    @ApiOperation(value="邮箱/手机激活")
    public Dto activeUser(String user,String code){
        return userService.activeUser(user,code);
    }

    //手机注册
    @PostMapping("/registerbyphone")
    @ApiOperation(value="手机注册")
    public Dto registerbyphone(@RequestBody User user) {
        return  userService.regeditByphone(user);
    }

    //用户登录 -->请求来源
    @PostMapping("/dologin")
    @ApiOperation(value = "登录功能")
    public Dto<ItripToken> login(String name, String password, Device device){
        String from = "pc";
        if (!device.isNormal()){
            from = "mobile";
        }
        return userService.selectByLogin(name,password,from);
    }

    //用户注销
    @GetMapping(value = "/logout",headers = "token")
    @ApiOperation(value = "注销功能")
    public Dto logout(HttpServletRequest request){
        String token = request.getHeader("token");
        return userService.logout(token);
    }

    //维护转换
    @PostMapping(value = "retoken",headers = "token")
    @ApiOperation(value = "维护转换")
    public Dto<ItripToken> retoken(HttpServletRequest request){
        String token = request.getHeader("token");
        return userService.retoken(token);
    }
}
